package com.mycompany.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mycompany.crm.base.BaseService;
import com.mycompany.crm.dao.CustomerLinkmanMapper;
import com.mycompany.crm.query.CustomerLinkmanQuery;
import com.mycompany.crm.service.CustomerLinkmanService;
import com.mycompany.crm.utils.AssertUtil;
import com.mycompany.crm.utils.PhoneUtil;
import com.mycompany.crm.vo.CustomerLinkman;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jy
 * @version 1.0
 * @date 2022/5/2 10:41
 */
@Service
public class CustomerLinkmanServiceImpl extends BaseService<CustomerLinkman,Integer> implements CustomerLinkmanService{

    @Resource
    private CustomerLinkmanMapper customerLinkmanMapper;

    /**
     * 多条件分页查询联系人
     *
     * @param customerLinkmanQuery
     * @return
     */
    @Override
    public Map<String, Object> queryCustomerLinkmanByParams(CustomerLinkmanQuery customerLinkmanQuery) {
        Map<String, Object> map = new HashMap<>();
        // 开启分页
        PageHelper.startPage(customerLinkmanQuery.getPage(), customerLinkmanQuery.getLimit());
        // 得到对应分页对象
        PageInfo<CustomerLinkman> pageInfo = new PageInfo<>(customerLinkmanMapper.selectByParams(customerLinkmanQuery));
        // 设置map对象
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        // 设置分页好的列表
        map.put("data",pageInfo.getList());
        return map;
    }

    /**
     * 添加联系人
     *
     * @param customerLinkman
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addCustomerContact(CustomerLinkman customerLinkman) {
        customerLinkman.setIsValid(1);
        customerLinkman.setCreateDate(new Date());
        customerLinkman.setUpdateDate(new Date());
        AssertUtil.isTrue(!PhoneUtil.isMobile(customerLinkman.getPhone()),"电话号码格式不正确！");
        /* 3. 执行添加操作，判断受影响的行数 */
        AssertUtil.isTrue(customerLinkmanMapper.insertSelective(customerLinkman) < 1, "添加联系人失败！");
    }

    /**
     * 更新联系人
     *
     * @param customerLinkman
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateCustomerContact(CustomerLinkman customerLinkman) {
        //* 1. 参数校验 *//*
        AssertUtil.isTrue(null == customerLinkman.getId(), "待更新记录不存在！");
        // 通过客户ID查询待更新的联系人
        CustomerLinkman customerLinkman1 = customerLinkmanMapper.selectByPrimaryKey(customerLinkman.getId());
        // 判断交往记录是否存在
        AssertUtil.isTrue(null == customerLinkman1, "待更新记录不存在！");
        //* 2. 设置参数的默认值  *//*
        assert customerLinkman1 != null;
        customerLinkman1.setLinkName(customerLinkman.getLinkName());
        customerLinkman1.setSex(customerLinkman.getSex());
        customerLinkman1.setZhiwei(customerLinkman.getZhiwei());
        customerLinkman1.setOfficePhone(customerLinkman.getOfficePhone());
        customerLinkman1.setPhone(customerLinkman.getPhone());
        customerLinkman1.setUpdateDate(new Date());
        AssertUtil.isTrue(!PhoneUtil.isMobile(customerLinkman.getPhone()),"电话号码格式不正确！");
        //* 3. 执行更新操作，判断受影响的行数 *//*
        AssertUtil.isTrue(customerLinkmanMapper.updateByPrimaryKeySelective(customerLinkman1) < 1, "修改联系人失败！");
    }

    /**
     * 删除联系人
     *
     * @param id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteCustomerLinkman(Integer id) {
        // 判断id是否为空，数据是否存在
        AssertUtil.isTrue(null == id, "待删除记录不存在！");
        // 通过id查询联系人
        CustomerLinkman customerLinkman = customerLinkmanMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(null == customerLinkman, "待删除记录不存在！");
        // 设置状态为失效
        assert customerLinkman != null;
        customerLinkman.setIsValid(0);
        customerLinkman.setUpdateDate(new Date());
        // 执行删除（更新）操作，判断受影响的行数
        AssertUtil.isTrue(customerLinkmanMapper.updateByPrimaryKeySelective(customerLinkman) < 1, "删除交往记录失败！");
    }


}
